#ifndef _EXTEND_H
#define _EXTEND_H

#include <types.h>

#define CLIPPER void pascal

#undef VT_ARRAY

#define VT_NIL       0x0000
#define VT_NUMERIC   0x0002
#define VT_FLOAT     0x0008
#define VT_DATE      0x0020
#define VT_LOGICAL   0x0080
#define VT_SYMBOL    0x0100
#define VT_ALIAS     0x0200
#define VT_BLOCK     0x1000
#define VT_CHARACTER 0x0400
#define VT_VREF      0x2000
#define VT_ARRAY     0x8000
#define VT_OBJECT    VT_ARRAY

#define IF(x,y,z) ((x)?(y):(z))

// Functions scope
#define FS_PUBLIC     0

typedef void * POVM;

typedef struct
{
   BYTE   szName[ 11 ];
   BYTE   bScope;
   LPVOID pVoid;
} SYMBOL, * PSYMBOL;

typedef struct
{
   WORD    wArea;     // Workarea number
   WORD    wMemvar;   // Index number into memvars ( publics & privates ) array
   PSYMBOL pSymbol;   // Holds OBJ symbols dynamicaly created
   LPVOID  pFuncPtr;  // Pointer to native code of a function
} DYNSYM, * PDYNSYM;

typedef struct
{
   WORD    wType;
   WORD    wClass;
   LONG    lLen;
   int     iValue;
   float   fValue;
   BOOL    bValue;
   WORD    wLine;
   LPVOID  lbase;
   LPSTR   szText;
   WORD    pcount;
   BYTE    date[ 9 ];
   LPVOID  pVoid;
   LPVOID  pbase;
   PSYMBOL pSymbol;
   POVM    pOVM;
} CLIPVAR, * PCLIPVAR;

typedef struct
{
   PSYMBOL pSymbols;
   LONG    lSize;
} SYMPITEM, * PSYMPITEM;

// Arrays management
void _Array( PCLIPVAR pDest, LONG lLength );
void _AAdd( PCLIPVAR pArray, PCLIPVAR pValue );
void _AGet( PCLIPVAR pArray, LONG lIndex, WORD wMask, PCLIPVAR pDest );
void _ASet( PCLIPVAR pArray, LONG lIndex, PCLIPVAR pValue );
void _ASetStr( PCLIPVAR pArray, LONG lIndex, LPSTR szText );
void _ASize( PCLIPVAR pArray, LONG lSize );
LONG _ALen( PCLIPVAR pArray );

void _bcopy( LPVOID pDest, LPVOID pOrg, WORD wLen );
void _bset( LPVOID pDest, BYTE bValue, WORD wLen );

// strings management
void _str( long lNumber, LPVOID buffer );
WORD _strat( LPSTR szFind, LPSTR szAt );
void _strcat( LPSTR szDest, LPSTR szOrg );
BOOL _strcmp( LPSTR szDest, LPSTR szOrg );
void _strcpy( LPSTR szDest, LPSTR szOrg );
BOOL _strempty( LPSTR szText );
WORD _strgreater( LPSTR sz1, LPSTR sz2 );
WORD _strlen( LPSTR szText );
void _strupper( LPSTR szDest, LPSTR szOrg );

// extend system

#define PCOUNT()  _pcount()
#define ISCHAR(x) _ischar(x)
#define ISNUM(x)  _isnum(x)

WORD _pcount( void );
PCLIPVAR _param( WORD wParam, WORD wMask );
LPSTR _parc( WORD wParam, ... );
WORD _parclen( WORD wParam, ... );
LPSTR _pard( WORD wParam, ... );
BOOL _parl( WORD, ... );
int  _parni( WORD, ... );
long _parnl( WORD, ... );
void _ret( void );
void _reta( LONG lLen );
void _retc( LPSTR szText );
void _retclen( LPSTR szText, WORD wLen );
void _retd( LPSTR szDate );
void _retl( BOOL bValue );
void _retni( int iValue );
void _retnl( long lValue );
void _storc( LPSTR szText, WORD wParam, ... );
void _storclen( LPSTR szText, WORD wLen, WORD wParam, ... );
void _stords( LPSTR szDate, WORD wParam, ... );
void _storni( int iValue, WORD wParam, ... );
void _stornl( LONG lValue, WORD wParam, ... );
void _isaveret( PCLIPVAR );

BOOL _ischar( WORD );
BOOL _isnum( WORD );

typedef void ( * PREGFUNC ) ( PSYMBOL );

// Error management

PCLIPVAR _errNew( void );
WORD     _errLaunch( PCLIPVAR pError );
void     _errPutDescription( PCLIPVAR pError, LPSTR szDescription );

// Memory management

LPVOID _vmAlloc( LONG lSize );
LPVOID _vmLock( POVM );
LPVOID _vmRealloc( POVM, LONG lSize );
LONG   _vmSize( POVM );
void   _vmUnLock( POVM );
void   _vmFree( POVM );

LPVOID _xgrab( LONG lSize );

// Dynamic symbol management

WORD _weed_eatr( PREGFUNC pRegFunc );

// Calling Five from C

PDYNSYM _GetDynSym( LPSTR szName );
void _Push( PCLIPVAR );
void _PutDynSym( PDYNSYM );
void _PutC( LPSTR szText );
void _PutQ( int iValue );
void _PutLN( long lValue );
void _PutNil( void );
void _Send( WORD wParams );
void _do( WORD wParams );

// files management

#define FO_READ       0
#define FO_WRITE      1
#define FO_READWRITE  2
#define FO_EXCLUSIVE 16
#define FO_DENYWRITE 32
#define FO_DENYREAD  48
#define FO_SHARED    64

HANDLE _fopen( LPSTR szFileName, DWORD wMode );
LONG   _fread( HANDLE hFile, LPVOID pBuffer, DWORD lBytes );
LONG   _fwrite( HANDLE hFile, LPVOID pBuffer, DWORD lBytes );
BOOL   _fclose( HANDLE hFile );
DWORD  _fseek( HANDLE hFile, LONG lOffset, DWORD dwOrigin );
BOOL   _ferase( LPSTR szFileName );
void   LogFile( LPSTR szFileName, LPSTR szText );

// user defined msgs

void MsgInfo( LPSTR, LPSTR );
void MsgAlert( LPSTR, LPSTR );

// DbfCdx RDD

BOOL  FieldGetL( WORD wField );
WORD  FieldLen( WORD wField );
LPSTR FieldName( WORD wField );
WORD  FieldPos( LPSTR szField );
BYTE  FieldType( WORD wField );

// Objects management

LPVOID GetMethod( WORD wClass, PDYNSYM pMessage );

#endif
